package client.gui.panels;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import controllers.ControllerIF;
import controllers.TrackTicketController;

public class TrackTicket extends JPanel implements ActionListener, PanelIF {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Instance of the panel's controller
	 */
	private TrackTicketController trackTicketController;
	
	
	private Long requestedTicketId;
	
	
	private Font font = new Font("Arial", Font.PLAIN, 16);
	
	private JPanel bottomPanel;
	private JButton buttonGo;
	private TextField ticketIdTextField;
	private JLabel ticketStageLabel;
	private JLabel ticketDateLabel;
	private JLabel datelbl;
	private JLabel stagelbl;

	
	public TrackTicket() {
		initGUI();
	}
	
	/**
	 * Initialize the UI
	 */
	public void initGUI() {

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		add(Box.createVerticalStrut(30));

		Panel up_panel = new Panel();
		add(up_panel);
		up_panel.setLayout(new BoxLayout(up_panel, BoxLayout.Y_AXIS));

		JPanel promptPanel = new JPanel();
		promptPanel.setMaximumSize(new Dimension(32767, 50));
		up_panel.add(promptPanel);
		promptPanel.setLayout(new BoxLayout(promptPanel, BoxLayout.X_AXIS));

		promptPanel.add(Box.createHorizontalStrut(20));

		JLabel lblNewLabel = new JLabel("Track ticket by ID");
		lblNewLabel.setFont(new Font("Arial", Font.BOLD, 13));
		promptPanel.add(lblNewLabel);

		promptPanel.add(Box.createHorizontalGlue());

		up_panel.add(Box.createVerticalStrut(15));

		JPanel inputPanel = new JPanel();
		inputPanel.setMaximumSize(new Dimension(32767, 8000));
		up_panel.add(inputPanel);
		inputPanel.setPreferredSize(new Dimension(10, 10));
		inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.X_AXIS));

		inputPanel.add(Box.createHorizontalStrut(20));

		JLabel promptLabel = new JLabel("Insert ticket ID:");
		promptLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		promptLabel.setFont(new Font("Arial", Font.BOLD, 13));
		inputPanel.add(promptLabel);

		inputPanel.add(Box.createHorizontalStrut(20));

		ticketIdTextField = new TextField();
		ticketIdTextField.setColumns(10);
		ticketIdTextField.setFont(new Font("Arial", Font.PLAIN, 13));
		ticketIdTextField.setMaximumSize(new Dimension(5170, 20));
		inputPanel.add(ticketIdTextField);

		inputPanel.add(Box.createHorizontalStrut(70));

		buttonGo = new JButton("GO");
		buttonGo.setAlignmentX(Component.CENTER_ALIGNMENT);
		buttonGo.addActionListener(this);
		inputPanel.add(buttonGo);

		inputPanel.add(Box.createHorizontalGlue());

		bottomPanel = new JPanel();
		bottomPanel.setVisible(false);

		add(Box.createVerticalGlue());
		
		add(bottomPanel);
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));

		Panel stagePanel = new Panel();
		stagePanel.setMaximumSize(new Dimension(32767, 150));
		bottomPanel.add(stagePanel);
		stagePanel.setLayout(new BoxLayout(stagePanel, BoxLayout.X_AXIS));

		stagePanel.add(Box.createHorizontalGlue());

		stagelbl = new JLabel("The current stage of the ticket is ");
		stagelbl.setAlignmentX(Component.CENTER_ALIGNMENT);
		stagelbl.setFont(font);
		stagePanel.add(stagelbl);

		ticketStageLabel = new JLabel();
		ticketStageLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		ticketStageLabel.setFont(font);
		stagePanel.add(ticketStageLabel);
		
				datelbl = new JLabel(" and it is suppose to end by ");
				stagePanel.add(datelbl);
				datelbl.setAlignmentX(Component.CENTER_ALIGNMENT);
				datelbl.setFont(font);
		
				ticketDateLabel = new JLabel();
				stagePanel.add(ticketDateLabel);
				ticketDateLabel.setFont(font);
				ticketDateLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

		stagePanel.add(Box.createHorizontalGlue());

		add(Box.createVerticalGlue());
		
		Component verticalGlue_1 = Box.createVerticalGlue();
		add(verticalGlue_1);

	}

	public void InsertDetails(String ticketStage, String dueDate) {
		if(ticketStage.equals("EVALUATION")) {
			ticketStageLabel.setText("evaluation");
			ticketDateLabel.setText(dueDate);
			bottomPanel.setVisible(true);
		}
		else if(ticketStage.equals("REVIEW")) {
			ticketStageLabel.setText("review");
			ticketDateLabel.setText(dueDate);
			bottomPanel.setVisible(true);
		}
		else if(ticketStage.equals("EXECUTION")) {
			ticketStageLabel.setText("execution");
			ticketDateLabel.setText(dueDate);
			bottomPanel.setVisible(true);
		}
		else if(ticketStage.equals("VERIFICATION")) {
			ticketStageLabel.setText("verification");
			ticketDateLabel.setText(dueDate);
			bottomPanel.setVisible(true);
		}
		else {
			stagelbl.setText("No information about ticket");
			ticketStageLabel.setText("");
			datelbl.setText("");
			bottomPanel.setVisible(true);
		}
	}
	
	/**
	 * Get the user's requested ticket's ID
	 * @return
	 */
	public Long getRequestedTicketId() {
		return requestedTicketId;
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == buttonGo) {
			if (ticketIdTextField.getText().isEmpty()) {
				JOptionPane.showMessageDialog(this, "Please insert the ticket ID number", "Error", JOptionPane.ERROR_MESSAGE);
			}
			else {
				try {
					requestedTicketId = Long.parseLong(ticketIdTextField.getText());
					trackTicketController.action();
				} catch (NumberFormatException nfe) {
					JOptionPane.showMessageDialog(this, "Given ticket ID is not a number", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}

	@Override
	public void setController(ControllerIF cif) {
		this.trackTicketController = (TrackTicketController) cif;
	}
}
